#! /usr/bin/env python
# -*- coding: utf-8 -*-
# __author__ = "Q1mi"
# Date: 2020/8/6
from django.http import JsonResponse
from django.utils import six
from rest_framework import status
from rest_framework.response import Response

from utils.response.httpstate import CODE_SUCCESS, MSG_SUCCESS


class BaseResponse(Response):
    def __init__(self, code=CODE_SUCCESS, message=MSG_SUCCESS, data={},page=None,params=[], status=status.HTTP_200_OK,
                 template_name=None, headers=None, exception=False, content_type='application/json'):
        super(BaseResponse, self).__init__(None, status=status)
        self._code = code
        self._message = message
        self._data = data

        if page:
            self.data = {"status": status, "code": code, "message": message, 'page':page,"data": data,"params":params}
        else:
            self.data = {"status": status, "code": code, "message": message, "data": data}
        self.template_name = template_name
        self.exception = exception
        self.content_type = content_type

        if headers:
            for name, value in six.iteritems(headers):
                self[name] = value

    @property
    def code(self):
        return self._code

    @code.setter
    def code(self, value):
        self._code = value

    @property
    def message(self):
        return self._message

    @message.setter
    def message(self, value):
        self._message = value

    @property
    def data(self):
        return self._data

    @data.setter
    def data(self, value):
        self._data = value

class BaseJsonResponse(JsonResponse):

    def __init__(self, code=CODE_SUCCESS, message=MSG_SUCCESS, data={}, page=None,status=status.HTTP_200_OK,safe=True,
                 template_name=None, headers=None, exception=False, content_type='application/json'):
        # data={"status": status, "code": code, "message": message, "data": data}
        if page:
            self.data = {"status": status, "code": code, "message": message, 'page':page,"data": data}
        else:
            self.data = {"status": status, "code": code, "message": message, "data": data}
        super(BaseJsonResponse, self).__init__(data=self.data,safe=safe)
        self._code = code
        self._message = message
        self._data = self.data

        self.template_name = template_name
        self.exception = exception
        self.content_type = content_type

        if headers:
            for name, value in six.iteritems(headers):
                self[name] = value

    @property
    def code(self):
        return self._code

    @code.setter
    def code(self, value):
        self._code = value

    @property
    def message(self):
        return self._message

    @message.setter
    def message(self, value):
        self._message = value

    @property
    def data(self):
        return self._data

    @data.setter
    def data(self, value):
        self._data = value

